package main

import (
	"fmt"
	"time"
)

//import "sync"
//var wg sync.WaitGroup

func strWorker(ch chan string) {
	time.Sleep(1 * time.Second)
	fmt.Println("do something with strWorker...")
	ch <- "str"
}

func intWorker(ch chan int) {
	time.Sleep(2 * time.Second)
	fmt.Println("do something with intWorker...")
	ch <- 1
}

func worker(done chan bool) {
	fmt.Println("start working...")
	done <- true
	fmt.Println("end working...")
}

func main() {
	breakTest()
	/*
		done := make(chan bool, 1)
		worker(done)
		//go worker(done)
		//<-done
		ch := make(chan string) //非缓冲通道
		//需要注意的是，程序中必须同时有不同的goroutine对非缓冲通道进行发送和接受操作，否则会造成阻塞。
		go func() {
			ch <- "ping"
		}()
		x := <-ch
		fmt.Println(x)
	*/

	/*
		chStr := make(chan string)
		chInt := make(chan int)
		go strWorker(chStr)
		go intWorker(chInt)

		for i := 0; i < 2; i++ {
			select {
			case <-chStr:
				fmt.Println("get value from strWorker")
			case <-chInt:
				fmt.Println("get value from intWorker")
			}
		}
	*/

	/*
		wg.Add(1)
		go testEveryOne (20)
		wg.Wait()
		// for i := 1; i < 200; i++ {
		// 	go testEveryOne (i)
		// }
	*/
}

func breakTest() {
	for i := 1; i < 5; i++ {
		if i == 3 {
			break
		}
		fmt.Printf("本次循环次数为：%v\n", i)
	}
}

func testEveryOne(n int) {
	for i := 1; i < n; i++ {
		// go testEveryOne (i)
		fmt.Println("forDemo:", i)
	}
	//wg.Done()
	// defer wg.Done()
	//加defer和不加defer的区别？
}
